<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="../CSS/注册.css">
</head>

<body>
  <h1>注册页面</h1>
  <form>

    <label>
      用户名: <input class="name" type="text">
    </label>
    <label>
      密码: <input class="pwd" type="password">
    </label>
    <label>
      确认密码: <input class="rpwd" type="password">
    </label>
    <label>
      昵称: <input class="nickname" type="text">
    </label>
    <label for="">
      <a href="./登录.html">已有账号，点击登录</a>
    </label>
    <button>注册</button>
  </form>
  <script src="../axios.min.js"></script>
  <script src="../http.js"></script>
  <script>
    const form = document.querySelector('form');
    const name = document.querySelector('.name');
    const pwd = document.querySelector('.pwd');
    const rpwd = document.querySelector('.rpwd');
    const nickname = document.querySelector('.nickname');
    const span = document.querySelector('span');

    form.addEventListener('submit', async function (e) {
      // 阻止表单默认提交
      e.preventDefault();
      if (name.value === '' || pwd.value === '' || rpwd.value === '' || nickname.value === '') {
        alert('请填写完整信息');
        return;
      }
      if (pwd.value !== rpwd.value) {
        alert('两次密码输入不一致');
        return;
      }
      // 发送请求
      let res = await http.post('/users/register', { username: name.value, password: pwd.value, rpassword: rpwd.value, nickname: nickname.value })
      if (res.data.code != 1) {
        alert(res.data.message);
      } else {
        alert(res.data.message)
        location.href = './登录.html';
      } 
      

    })
  </script>
</body>

</html>